package com.xpec.c4.game.datamanager.agent;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.service.module.account.Account;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.DataManager;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.game.login.LoginModule;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.module.BasicServiceModule;

/**
 * 
 * @author yangbingyan
 *
 */
public class AccountDataAgent extends DataManager<AccountData>{
	
	private static final Log log = LogFactory.getLog(AccountDataAgent.class);

	/**
	 * 
	 * @param module
	 */
	public AccountDataAgent(BasicServiceModule module) {
		super(module);	
	}

	/**
	 * 
	 * @param guid
	 * @return
	 */
	public Account getAccount(GUID id){
		if(this.getData(id) != null){
			return (Account) this.getData(id).getData();
		}
		return null;
	}

	/**
	 * 向DataCenter获取指定Player数据<br>
	 * 
	 * @param guid
	 */
	public boolean getAccountDataFromDC(GUID id, Object... userData){
		
		AccountData data = new AccountData();
		
		data.setKey(id);
		
		data.setMessageType(MessageTypes.QUERY_ACCOUNT_INFO);
		
		// 產生callback需要的method
		Method mth = getCallbackMethod(this, "queryDataCallback");
		if(log.isInfoEnabled()){
			log.info(LoginModule.LOGIN_TRACE_LOG_STRING+" get AccountData from DC, a: "+id.toString());
		}
		return super.dataRequest(this, null, null, this, mth, data, userData);
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	public boolean createAccountToDC(GUID str, Object accountObj,
			Object... userData) {

		AccountData data = new AccountData();
	
		data.setMessageType(MessageTypes.CREATE_ACCOUNT_INFO);
		data.setData(accountObj);

		// 產生callback需要的method
		Method mth = getCallbackMethod(this, "createAccountCallback");

		return super.dataRequest(this, null, null, this, mth, data, accountObj, userData);
	}

	/**
	 * callback
	 */
	public void queryDataCallback(BaseDataInfo obj, Object... userObject){
		
		switch(obj.getResultCode()){
		
		case ResultNo.OK:
		{
			
			this.putData(obj.getKey(), obj.getHandle(), (AccountData) obj);

			break;
		}
			
		case ResultNo.ACCESS_DENY:
			log.error("AccountDataAgent::queryDataCallback :Access deny id: " + obj.getKey());
			break;
			
		case ResultNo.FAILED:
			log.error("AccountDataAgent::queryDataCallback :Query failed id: " + obj.getKey());
			break;
			
		case ResultNo.DATA_NOT_FOUND:
			log.error("AccountDataAgent::queryDataCallback :Data not found id: " + obj.getKey());
			break;
			
		case ResultNo.DB_OPERATION_FAILED :
			log.error("AccountDataAgent::queryDataCallback :DB operation failed id: " + obj.getKey());
			break;
		}
		
	}
	
	/**
	 * callback
	 */
	public void createAccountCallback(BaseDataInfo obj, Object... userObject){
	
		switch(obj.getResultCode()){
		
		case ResultNo.OK:
		{
			// AccountData q = (AccountData)obj;
			
			if(userObject != null && userObject[0] != null && (userObject[0] instanceof String)){
				
				System.out.println("id: " + obj.getKey() + " " + (String)(userObject[0]) );

			}else{
				// System.out.println("id: " + q.getKey() + " name:" );
			}
		
			break;
		}

		case ResultNo.ACCESS_DENY:
			System.out.println("Access deny  id: " + obj.getKey());
			break;
			
		case ResultNo.FAILED:
			System.out.println("Create failed  id: " + obj.getKey());
			break;
			
		case ResultNo.DB_OPERATION_FAILED:
			System.out.println("DB Create failed  id: " + obj.getKey());
			break;
			
		}
	}
	
	
}

